//
//  WmcImgTextControl.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/3/1.
//

import UIKit
import YPBedrock

class WmcImgTextControl: UIControl {
    // Lifecycle

    override init(frame: CGRect) {
        super.init(frame: frame)
        config()
        layoutImgText()
    }

    convenience init(title: String, image: UIImage) {
        self.init(frame: .zero)
        label.text = title
        imageView.image = image
    }

    public required convenience init?(coder: NSCoder) {
        self.init(frame: .zero)
    }

    // Internal

    lazy var label: UILabel = {
        let label = UILabel()
        label.textAlignment = .center
        label.textColor = .white
        addSubview(label)
        return label
    }()

    lazy var imageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .scaleAspectFit
        addSubview(imageView)
        return imageView
    }()

    /// 标题
    var title: String? {
        set { label.text = newValue }
        get { return label.text }
    }

    /// 字色
    var textColor: UIColor {
        set { label.textColor = newValue }
        get { return label.textColor }
    }

    /// 字体字号
    var font: UIFont {
        set { label.font = newValue }
        get { return label.font }
    }

    /// 对齐方式
    var textAlignment: NSTextAlignment {
        set { label.textAlignment = newValue }
        get { return label.textAlignment }
    }

    /// 图片和文字的间距
    var imgTextSpacing: CGFloat = 0 {
        didSet { layoutImgText() }
    }

    /// 内容间距
    var contentEdge: UIEdgeInsets = .zero {
        didSet { layoutImgText() }
    }

    /// 图片大小
    var imgSize: CGFloat? {
        didSet { layoutImgText() }
    }

    /// 图片标题渲染颜色
    func setTintColor(_ color: UIColor) {
        label.textColor = color
        imageView.yp.set(tintColor: color)
    }

    /// 图片渲染颜色
    func setImageTintColor(_ color: UIColor) {
        imageView.yp.set(tintColor: color)
    }
    
    func setTextColor(_ color: UIColor) {
        textColor = color
    }

    /// 子类进行约束布局
    func layoutImgText() {}

    /// 配置基础
    func config() {}
}
